package co.yixiang.modules.system.rest;

import lombok.AllArgsConstructor;
import cn.hutool.core.convert.Convert;
import co.yixiang.logging.aop.log.Log;
import co.yixiang.modules.system.domain.GdxConfig;
import co.yixiang.modules.system.service.GdxConfigService;
import co.yixiang.modules.system.dto.queryCriteria.GdxConfigQueryCriteria;
import co.yixiang.modules.system.dto.GdxConfigDto;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
* @author lww
* @date 2020-10-23
*/
@AllArgsConstructor
@Api(tags = "机大呷配置管理")
@RestController
@RequestMapping("/api/gdxConfig")
public class GdxConfigController {

    private final GdxConfigService gdxConfigService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('admin','gdxConfig:list')")
    public void download(HttpServletResponse response, GdxConfigQueryCriteria criteria) throws IOException {
        gdxConfigService.download(Convert.toList(GdxConfigDto.class, gdxConfigService.queryAll(criteria)), response);
    }

    @GetMapping
    @Log("查询机大呷配置")
    @ApiOperation("查询机大呷配置")
    @PreAuthorize("@el.check('admin','gdxConfig:list')")
    public ResponseEntity<Object> getGdxConfigs(GdxConfigQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(gdxConfigService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增机大呷配置")
    @ApiOperation("新增机大呷配置")
    @PreAuthorize("@el.check('admin','gdxConfig:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody GdxConfig resources){
        return new ResponseEntity<>(gdxConfigService.save(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改机大呷配置")
    @ApiOperation("修改机大呷配置")
    @PreAuthorize("@el.check('admin','gdxConfig:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody GdxConfig resources){
        gdxConfigService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除机大呷配置")
    @ApiOperation("删除机大呷配置")
    @PreAuthorize("@el.check('admin','gdxConfig:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody List<String> ids) {
        gdxConfigService.removeByIds(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}